﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Xml;
using System.Net;
using System.IO;

namespace WindowsClient.Classes
{
    public enum Result
    {
        ErrorUnknown = -3,
        ErrorArgument = -2,
        ErrorFail = -1,
        SuccessOk = 0,
        SuccessFalse = 1
    };

    public class Validation
    {
        public static bool OfResult(Result r)
        {
            if (r >= 0)
                return true;
            else
                return false;
        }
    };

    class Http
    {
        public static Result MakeGetRequest(string url, ref String response)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";
                HttpWebResponse webresponse = request.GetResponse() as HttpWebResponse;
                StreamReader reader = new StreamReader(webresponse.GetResponseStream());
                response = reader.ReadToEnd();
                return Result.SuccessOk;
            }
            catch (Exception e)
            {
                response = e.Message;
                return Result.ErrorFail;
            }
        }

        public static Result MakePostRequest(string url, byte [] postData, ref String response)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postData.Length;
                Stream postStream = request.GetRequestStream();
                postStream.Write(postData, 0, postData.Length);
                postStream.Close();
                HttpWebResponse webresponse = request.GetResponse() as HttpWebResponse;
                StreamReader reader = new StreamReader(webresponse.GetResponseStream());
                response = reader.ReadToEnd();
                return Result.SuccessOk;
            }
            catch (Exception e)
            {
                response = e.Message;
                return Result.ErrorFail;
            }
        }

        public static Result MakePutRequest(string url, byte [] putData, ref String response)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "PUT";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = putData.Length;
                Stream postStream = request.GetRequestStream();
                postStream.Write(putData, 0, putData.Length);
                postStream.Close();
                HttpWebResponse webresponse = request.GetResponse() as HttpWebResponse;
                StreamReader reader = new StreamReader(webresponse.GetResponseStream());
                response = reader.ReadToEnd();
                return Result.SuccessOk;
            }
            catch (Exception e)
            {
                response = e.Message;
                return Result.ErrorFail;
            }
        }

        public static Result MakeDeleteRequest(string url, ref String response)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "DELETE";
                HttpWebResponse webresponse = request.GetResponse() as HttpWebResponse;
                StreamReader reader = new StreamReader(webresponse.GetResponseStream());
                response = reader.ReadToEnd();
                return Result.SuccessOk;
            }
            catch (Exception e)
            {
                response = e.Message;
                return Result.ErrorFail;
            }
        }
    }
}
